package com.itheima;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.feign.WmUserFeign;
import com.itheima.wemedia.pojo.WmUser;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @version 1.0
 * @description 描述
 * @package com.itheima
 */
@SpringBootApplication
@MapperScan(basePackages = "com.itheima.user.mapper")
@EnableFeignClients(basePackages = "com.itheima.*.feign")
public class UserApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(UserApplication.class, args);
        /*WmUserFeign myBean = app.getBean("myWmUserFeign", WmUserFeign.class);
        String[] beanNamesForType = app.getBeanNamesForType(WmUserFeign.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }
        ResultVo<WmUser> byApUserId = myBean.getByApUserId(1l);
        System.out.println(byApUserId);*/
    }

    //@Bean
    public WmUserFeign myWmUserFeign(@Autowired final RestTemplate restTemplate){
        WmUserFeign myBean = (WmUserFeign)Proxy.newProxyInstance(UserApplication.class.getClassLoader(), new Class[]{WmUserFeign.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                GetMapping getMapping = method.getAnnotation(GetMapping.class);
                ResultVo resultVo = null;
                if (null != getMapping) {
                    String springApplicationName = WmUserFeign.class.getAnnotation(FeignClient.class).name();
                    String[] url = getMapping.value();
                    String httpUrl = "http://" + springApplicationName + "/" + url[0];
                    resultVo = restTemplate.getForObject(httpUrl, ResultVo.class, args);
                }
                return resultVo;
            }
        });
        return myBean;
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}